home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0033_STARS1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  109 lines

  1. {
  2. BERNIE PALLEK
  3.  
  4. > Hmm.. does anyone have an example of a starfield routine in Turbo Pascal..
  5.  
  6. OK, here's a sample (I don't know what kind of starfield you're looking for):
  7.  
  8. {EGA/VGA parallax stars}
  9.  
  10. Uses
  11.   Crt, Graph, KasUtils;
  12.  
  13. Const
  14.   starCol : Array[0..2] of Byte = (8, 7, 15);
  15.  
  16. Type
  17.   StarRec = Record
  18.     x : Integer;
  19.     y : Integer;
  20.     d : Integer;  { depth }
  21.   end;
  22.  
  23. Var
  24.   stars : Array[0..31] of StarRec;
  25.   xinc,
  26.   yinc  : Integer;
  27.   ch    : Char;
  28.  
  29.  
  30. Procedure OpenGraph;
  31. Var
  32.   gd, gm : Integer;
  33. begin
  34.   EgaVga_Exe;
  35.   Gd := Detect;
  36.   { this doesn't care if you don't have correct video card or not }
  37.   InitGraph(gd, gm, '');   { put the path to your BGI }
  38. end;
  39.  
  40. Procedure InitStars;
  41. Var
  42.   i : Integer;
  43. begin
  44.   For i := 0 to 31 do
  45.   With stars[i] do
  46.   begin
  47.     x := Random(GetMaxX);
  48.     y := Random(GetMaxY);
  49.     d := Random(3);
  50.   end;
  51. end;
  52.  
  53. Procedure MoveStars;
  54. Var
  55.   i : Integer;
  56. begin
  57.   For i := 0 to 31 do
  58.   With stars[i] do
  59.   begin
  60.     PutPixel(x, y, 0);
  61.     x := x + xinc * (d + 1);
  62.     if (x < 0) then
  63.       x := x + GetMaxX;
  64.     if (x > GetMaxX) then
  65.       x := x - GetMaxX;
  66.     y := y + yinc * (d + 1);
  67.     if (y < 0) then
  68.       y := y + GetMaxY;
  69.     if (y > GetMaxY) then
  70.       y := y - GetMaxY;
  71.     PutPixel(x, y, starCol[d]);
  72.   end;
  73. end;
  74.  
  75. begin
  76.   OpenGraph;  (* enter Graphics mode *)
  77.   InitStars;
  78.   xinc := 1;
  79.   yinc := 0;
  80.   Repeat
  81.     MoveStars;
  82.     Delay(10);
  83.     (* Delay here For faster computers *)
  84.   Until KeyPressed;
  85.   ch := ReadKey;
  86.   if (ch = #0) then
  87.     ch := ReadKey;  (* get rid of extended keycodes *)
  88.   CloseGraph;
  89. end.
  90.  
  91. {
  92. Whew!  There you have it!  Untested, of course, so you may have to iron out a
  93. few bugs.
  94.  
  95. **** BIG HINT: You should probably use Real numbers instead of Integer numbers
  96. for x and y positions and increments, and Round them when PutPixel-ing!  This
  97. will allow you to make smoother transitions, as well as bouncing effects, and
  98. other neat stuff. ****
  99.  
  100. You'll notice (if the thing works) that the stars move horizontally only, and
  101. the dimmer ones move slower than the bright ones (parallax/multi-layered).  You
  102. can add extra layers, but remember to change the StarCol Constant so you have
  103. the right number of colours For the stars.
  104.  
  105. Sorry, I was too lazy to comment it thoroughly; I'm expecting that you'll be
  106. able to figure it out Without too much trouble.  Sorry if you can't; Write me
  107. for an explanation.  TTYL.
  108. }
  109.